package org.jeecg.modules.relation.relteachersubject.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.achexamination.entity.AchExamination;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.modules.relation.relclassstudent.vo.RelClassesAndStudentsVO;
import org.jeecg.modules.relation.relteachersubject.entity.RelTeacherSubject;
import org.jeecg.modules.relation.relteachersubject.service.IRelTeacherSubjectService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.relation.relteachersubject.vo.RelTeacherSubjectVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: rel_teacher_subject
 * @author: baochangrong
 * @Date: 2022-02-16
 * @Version: V1.0
 */
@Api(tags = "rel_teacher_subject")
@RestController
@RequestMapping("/rel_teacher_subject/relTeacherSubject")
@Slf4j
public class RelTeacherSubjectController extends JeecgController<RelTeacherSubject, IRelTeacherSubjectService> {
    @Autowired
    private IRelTeacherSubjectService relTeacherSubjectService;
    @Autowired
    private ISysInfo sysInfo;

    /**
     * 分页列表查询
     *
     * @param relTeacherSubject
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "rel_teacher_subject-分页列表查询")
    @ApiOperation(value = "rel_teacher_subject-分页列表查询", notes = "rel_teacher_subject-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(RelTeacherSubject relTeacherSubject,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
//        QueryWrapper<RelTeacherSubject> queryWrapper = QueryGenerator.initQueryWrapper(relTeacherSubject, req.getParameterMap());
        QueryWrapper<RelTeacherSubject> queryWrapper = QueryGenerator.initQueryWrapper(relTeacherSubject, req.getParameterMap());
        Page<RelTeacherSubject> page = new Page<RelTeacherSubject>(pageNo, pageSize);
        IPage<RelTeacherSubject> pageList = relTeacherSubjectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "/volist")
    public Result<?> queryPageVOList(RelTeacherSubject relTeacherSubject,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {
        String tenantId = req.getHeader("tenant-id");
        relTeacherSubject.setTenantId(tenantId);
        Page<RelTeacherSubjectVO> page = new Page<>(pageNo, pageSize);
        IPage<RelTeacherSubjectVO> pageList = relTeacherSubjectService.queryVo(page, relTeacherSubject);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param relTeacherSubject
     * @return
     */
    @AutoLog(value = "rel_teacher_subject-添加")
    @ApiOperation(value = "rel_teacher_subject-添加", notes = "rel_teacher_subject-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody RelTeacherSubject relTeacherSubject) {
        relTeacherSubjectService.save(relTeacherSubject);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param relTeacherSubject
     * @return
     */
    @AutoLog(value = "rel_teacher_subject-编辑")
    @ApiOperation(value = "rel_teacher_subject-编辑", notes = "rel_teacher_subject-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody RelTeacherSubject relTeacherSubject) {
        relTeacherSubjectService.updateById(relTeacherSubject);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rel_teacher_subject-通过id删除")
    @ApiOperation(value = "rel_teacher_subject-通过id删除", notes = "rel_teacher_subject-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        relTeacherSubjectService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "rel_teacher_subject-批量删除")
    @ApiOperation(value = "rel_teacher_subject-批量删除", notes = "rel_teacher_subject-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.relTeacherSubjectService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rel_teacher_subject-通过id查询")
    @ApiOperation(value = "rel_teacher_subject-通过id查询", notes = "rel_teacher_subject-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        RelTeacherSubject relTeacherSubject = relTeacherSubjectService.getById(id);
        if (relTeacherSubject == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(relTeacherSubject);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param relTeacherSubject
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RelTeacherSubject relTeacherSubject) {
        return super.exportXls(request, relTeacherSubject, RelTeacherSubject.class, "rel_teacher_subject");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RelTeacherSubject.class);
    }

    @ApiOperation(value = "rel_teacher_subject-分页列表查询", notes = "rel_teacher_subject-分页列表查询")
    @GetMapping(value = "/queryTeacher")
    public Result<?> queryTeacherList(HttpServletRequest req) {
        String tenantId = req.getHeader("tenant-id");
        return Result.OK(relTeacherSubjectService.queryAllTeacher(tenantId));
    }

    @ApiOperation(value = "rel_teacher_subject-分页列表查询", notes = "rel_teacher_subject-分页列表查询")
    @GetMapping(value = "/querySubject")
    public Result<?> querySubjectList(HttpServletRequest req) {
        String tenantId = req.getHeader("tenant-id");
        return Result.OK(relTeacherSubjectService.queryAllSubject(tenantId));
    }

    @GetMapping(value = "/queryTeacherListBySubject")
    public Result<?> queryTeacherListBySubject(RelTeacherSubjectVO relTeacherSubjectVO,HttpServletRequest req) {
        //组装vo以外的其他参数
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
//        RelTeacherSubjectVO relTeacherSubjectVO = new RelTeacherSubjectVO();
//        relTeacherSubjectVO.setSubjectId(subjectId);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
        String tenantId = req.getHeader("tenant-id");
        relTeacherSubjectVO.setTenantId(tenantId);
        BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,relTeacherSubjectVO,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        //根据用户身份重新设置查询条件值，不同的用户身份查询过滤不同的数据：
        //1.获取用户身份 userIdentity loginUser.getUserIdentity() 用户身份字典码user_identity， 3教师 4学生 5家长 6班级  1普通用户 2上级部门负责人
        //2.获取用户id loginUser.getId()
        //userIdentity 用户身份字典码user_identity， 3教师 4学生 5家长 6班级  1普通用户 2上级部门负责人
//        relTeacherSubjectVO = reSetObjectValue(baseApiEntity, relTeacherSubjectVO);

        List<RelTeacherSubjectVO> list = relTeacherSubjectService.queryTeacherListBySubject(relTeacherSubjectVO);
        return Result.OK(list);
    }



}
